package org.ysu.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class YsuRequest extends HttpServletRequestWrapper {
    private Map<String,String[]> paramsMap;
    public YsuRequest(HttpServletRequest request) {
        super(request);
        Map<String,String[]> parameterMap = request.getParameterMap();
        paramsMap = new HashMap<>();
        for (Map.Entry<String,String[]> entry : parameterMap.entrySet()){
            paramsMap.put(entry.getKey(),entry.getValue());
        }
    }
    public void addParameter (String name,Object value) {

        if (paramsMap.containsKey(name)){
            String[] values = paramsMap.get(name);
            values = Arrays.copyOf(values,values.length+1);
            values[values.length-1] = String.valueOf(value);
        }else {
            paramsMap.put(name,new String[]{String.valueOf(value)});
        }
    }
    @Override
    public Map<String,String[]> getParameterMap(){
        return paramsMap;
    }

    @Override
    public String getParameter(String name){
        if (paramsMap.containsKey(name)){
            String[] values = paramsMap.get(name);
            return values.length>0 ? values[0] : null;
        }
        return null;
    }

    @Override
    public String[] getParameterValues(String name){
        if (paramsMap.containsKey(name)){
            return paramsMap.get(name);
        }
        return null;
    }
}
